<?php

    /**
     * @package      Modules
     * @subpackage   Admins_Classes_Customs
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
    class Admins_Classes_Customs_Rights implements Base_Classes_Customs_Interface
    {

    ////////////////////////////////////////////////////////////////////////////
    //                                 Methods                                //
    ////////////////////////////////////////////////////////////////////////////

        /**
         * Prepares form values.
         *
         * @param       DbBase      $element
         * @param       array       $params
         * @return      array
         */
        public static function prepare(DbBase $element, array $params = array())
        {
            $modules = Doctrine_Query::create()
                        ->from('DbModule m')
                        ->where('m.status = ?', DbBase::ENABLED)
                        ->addWhere('m.display = ?', true)
                        ->execute();

            $rights = array();
            foreach($element->DbRight as $right) {
                $rights[$right->module_id] = $right->module_id;
            }

            return array('modules' => $modules, 'rights' => $rights);
        }

        /**
         * Processes the element
         *
         * Throws a Base_Classes_Customs_Exception if the element is invalid (global validation).
         *
         * @throws      Base_Classes_Customs_Exception
         * @param       &DbBase     $element
         * @param       array       $params
         */
        public function process(DbBase &$element, array $params = array())
        {
            $modules =& $params['custom-'. __CLASS__ .'-modules'];

            if ($modules === null) {
                $modules = array();
            }

            $query = Doctrine_Query::create()
                        ->from('DbModule m')
                        ->where('m.status = ?', DbBase::ENABLED)
                        ->whereIn('m.id', (array) $modules)
                        ->execute();

            $element->DbRight->delete();
            $element->DbModule->merge($query);
        }

        /**
         * Validates elements fields
         * 
         * Throws a Base_Classes_Customs_Exception if field is invalid.
         *
         * @throws      Base_Classes_Customs_Exception
         * @param       DbBase     $element
         * @param       string      $fieldname
         * @param       mixed       $value
         */
        public function validate(DbBase $element, $fieldname, $value)
        {

        }
    }